package com.ytjj.qmyx.mall.service.impl;


import com.ytjj.qmyx.mall.enums.OrderStatusEnums;
import com.ytjj.qmyx.mall.enums.OrdersStatusTypeEnums;
import com.ytjj.qmyx.mall.mapper.OldSysDao;
import com.ytjj.qmyx.mall.mapper.ReturnGoodsMapper;
import com.ytjj.qmyx.mall.model.Expresslook;
import com.ytjj.qmyx.mall.model.ProductInfo;
import com.ytjj.qmyx.mall.model.ReturnGoods;
import com.ytjj.qmyx.mall.model.ReturnGoodsExample;
import com.ytjj.qmyx.mall.model.dto.OrdersInfoDto;
import com.ytjj.qmyx.mall.model.response.UserOrderResponse;
import com.ytjj.qmyx.mall.service.OldSysService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OldSysServiceImpl implements OldSysService {

    @Resource
    private OldSysDao oldSysDao;
    @Resource
    private ReturnGoodsMapper returnGoodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserOrderResponse> selectList(String phone) {

        List<UserOrderResponse> list = new ArrayList<>();
        List<OrdersInfoDto> ordersInfoDtos = oldSysDao.selectList(phone);
        if (!CollectionUtils.isEmpty(ordersInfoDtos)) {
            List<Integer> orderInfoIds = ordersInfoDtos.stream().filter(item -> item.getOrderInfoId() != null).map(OrdersInfoDto::getOrderInfoId).collect(Collectors.toList());
            List<ReturnGoods> returnGoodsList = null;
            if (!CollectionUtils.isEmpty(orderInfoIds)) {
                ReturnGoodsExample example = new ReturnGoodsExample();
                example.createCriteria().andOrdersInfoIdIn(orderInfoIds);
                example.setOrderByClause("id desc");
                returnGoodsList = returnGoodsMapper.selectByExample(example);
            }
            for (OrdersInfoDto ordersInfoDto : ordersInfoDtos) {

                Expresslook expresslook = new Expresslook();
                expresslook.setIsCheck(ordersInfoDto.getIsCheck());
                expresslook.setCom(ordersInfoDto.getCom());
                expresslook.setTrack(ordersInfoDto.getTrack());
                expresslook.setPhone(ordersInfoDto.getUserPhone());
                expresslook.setChcom(ordersInfoDto.getChcom());
                expresslook.setAddress(ordersInfoDto.getAddress());

                ProductInfo productInfo = new ProductInfo();
                productInfo.setBannerimg(ordersInfoDto.getProductImg());
                productInfo.setTitle(ordersInfoDto.getProductName());
                productInfo.setMain(ordersInfoDto.getDescr());
                productInfo.setOriginalprice(ordersInfoDto.getOriginalPrice());
                productInfo.setProductSkuSpec(ordersInfoDto.getProductSkuSpec());
                productInfo.setProductSpecId(ordersInfoDto.getProductSpecId());

                UserOrderResponse userOrderResponse = new UserOrderResponse();
                userOrderResponse.setId(ordersInfoDto.getOrderId());
                userOrderResponse.setOrderInfoId(ordersInfoDto.getOrderInfoId());
                userOrderResponse.setChildOrderNo(ordersInfoDto.getChildOrderNo());
                userOrderResponse.setPrice(ordersInfoDto.getPrice());
                userOrderResponse.setExpressId(Optional.ofNullable(ordersInfoDto.getExpressId()).orElse(0));
                userOrderResponse.setOrderId(ordersInfoDto.getOrderId());
                userOrderResponse.setStatus(ordersInfoDto.getOrderStatus());
                userOrderResponse.setWxpaystatus(ordersInfoDto.getIsPay());
                userOrderResponse.setExpresslook(expresslook);
                userOrderResponse.setProductInfo(productInfo);
                userOrderResponse.setCreatetime(ordersInfoDto.getCreateTime());
                userOrderResponse.setOrderNo(ordersInfoDto.getOrderNo());
                userOrderResponse.setType(OrdersStatusTypeEnums.getType(ordersInfoDto.getOrderStatus()));
                userOrderResponse.setOrderNo(ordersInfoDto.getOrderNo());
                userOrderResponse.setIsPay(ordersInfoDto.getIsPay());
                if (!StringUtils.isEmpty(ordersInfoDto.getOrderInfoStatus())
                        && ordersInfoDto.getOrderInfoStatus().equals("1") || ordersInfoDto.getOrderInfoStatus().equals("2")) {
                    userOrderResponse.setOrderDelay(ordersInfoDto.getOrderDelay());
                }
                if (!CollectionUtils.isEmpty(returnGoodsList)) {
                    List<ReturnGoods> collect = returnGoodsList.stream().filter(item -> item.getOrdersInfoId().equals(userOrderResponse.getOrderInfoId())).collect(Collectors.toList());
                    userOrderResponse.setServiceNo(CollectionUtils.isEmpty(collect) ? null : collect.get(0).getServiceNo());
                }
                list.add(userOrderResponse);
            }
        }
        return list;
    }
}
